import { IPanelFieldControllerParams } from '@/core/interface';
import { PanelDetailController } from './panel-detail-controller';

/**
 * 面板属性项控制器
 *
 * @export
 * @class PanelFieldController
 * @extends {PanelDetailModel}
 */
export class PanelFieldController extends PanelDetailController {
	/**
	 * 是否启用
	 *
	 * @type {boolean}
	 * @memberof PanelFieldController
	 */
	public disabled = false;

	/**
	 * 错误信息
	 *
	 * @type {string}
	 * @memberof PanelFieldController
	 */
	public error = '';

	/**
	 * 表单项启用条件
	 *
	 * 0 不启用
	 * 1 新建
	 * 2 更新
	 * 3 全部启用
	 *
	 * @type {(number | 0 | 1 | 2 | 3)}
	 * @memberof PanelFieldController
	 */
	public enableCond: number | 0 | 1 | 2 | 3 = 3;

	/**
	 * @description 默认状态
	 * 0 不启用
	 * 1 只读
	 * 2 禁用
	 * @type {('0' | '1' | '2')}
	 * @memberof PanelFieldController
	 */
	public fieldState: '0' | '1' | '2';

	/**
	 * 绑定数据项
	 *
	 * @type {(string | undefined)}
	 * @memberof PanelFieldController
	 */
	public dataItemName: string | undefined = undefined;
	/**
	 * 绘制模式
	 *
	 * @private
	 * @type {('TEXT' | 'HEADING1' | 'HEADING2' | 'HEADING3' | 'HEADING4' | 'HEADING5' | 'HEADING6' | 'PARAGRAPH')}
	 * @memberof PanelFieldController
	 */
	public renderMode:
		| 'TEXT'
		| 'HEADING1'
		| 'HEADING2'
		| 'HEADING3'
		| 'HEADING4'
		| 'HEADING5'
		| 'HEADING6'
		| 'PARAGRAPH' = 'TEXT';

	/**
	 * 换行模式{WRAP：换行、 NOWRAP：不换行 }
	 *
	 * @type {(string | 'WRAP' | 'NOWRAP')}
	 * @memberof PanelFieldController
	 */
	public wrapMode: string | 'WRAP' | 'NOWRAP' = 'WRAP';

	/**
	 * 文本垂直对齐模式{TOP：上对齐、 MIDDLE：居中、 BOTTOM：下对齐 }
	 *
	 * @type {(string | 'TOP' | 'MIDDLE' | 'BOTTOM')}
	 * @memberof PanelFieldController
	 */
	public vAlign: string | 'TOP' | 'MIDDLE' | 'BOTTOM' = 'MIDDLE';

	/**
	 * 文本水平对齐模式{LEFT：左对齐、 CENTER：居中、 RIGHT：右对齐、 JUSTIFY：两端对齐 }
	 *
	 * @type {(string | 'LEFT' | 'CENTER' | 'RIGHT' | 'JUSTIFY')}
	 * @memberof PanelFieldController
	 */
	public hAlign: string | 'LEFT' | 'CENTER' | 'RIGHT' | 'JUSTIFY' = 'LEFT';

	/**
	 * 必填
	 *
	 * @type {boolean}
	 * @memberof PanelFieldController
	 */
	public required: boolean;

	/**
	 * Creates an instance of PanelFieldController.
	 * PanelFieldController 实例
	 *
	 * @param {*} [IPanelFieldControllerParams]
	 * @memberof PanelFieldController
	 */
	constructor(opts: IPanelFieldControllerParams) {
		super(opts);
		this.disabled = opts.disabled ? true : false;
		this.enableCond = opts.enableCond;
		this.fieldState = opts.fieldState;
		this.dataItemName = opts.dataItemName;
		this.renderMode = opts.renderMode;
		this.wrapMode = opts.wrapMode ? opts.wrapMode : 'WRAP';
		this.vAlign = opts.vAlign ? opts.vAlign : 'MIDDLE';
		this.hAlign = opts.hAlign ? opts.hAlign : 'LEFT';
		this.required = opts.required;
	}

	/**
	 * 设置是否启用
	 *
	 * @param {boolean} state
	 * @memberof PanelFieldController
	 */
	public setDisabled(state: boolean): void {
		this.disabled = state;
	}

	/**
	 * 设置信息内容
	 *
	 * @param {string} error
	 * @memberof PanelFieldController
	 */
	public setError(error: string): void {
		this.error = error;
	}

	/**
	 * 设置是否启用
	 *
	 * @param {string} srfuf
	 * @memberof PanelFieldController
	 */
	public setEnableCond(srfuf: string): void {
		// 是否有权限
		const isReadOk = true;
		const _srfuf: number = parseInt(srfuf, 10);
		let state = true;

		if (isReadOk) {
			if (_srfuf === 1) {
				if ((this.enableCond & 2) === 2) {
					state = false;
				}
			} else {
				if ((this.enableCond & 1) === 1) {
					state = false;
				}
			}
		}
		this.setDisabled(state);
	}

	/**
	 * 加载数据
	 *
	 * @param {*} context
	 * @param {*} viewParams
	 * @memberof PanelFieldController
	 */
	public async load(context: any, viewParams: any) {
		if (this.parentName) {
			let parentItem =
				this.panel.store.layoutModelDetails[`${this.parentName}_${this.index}`];
			// 尝试找
			if (!parentItem) {
				parentItem = this.panel.store.layoutModelDetails[this.parentName];
			}
			if (parentItem && parentItem.getData()) {
				const parentItemData = parentItem.getData();
				const key = this.dataItemName ? this.dataItemName : this.name;
				if (parentItemData) {
					this.data = parentItemData[key];
				}
			}
		}
	}

	/**
	 * 设置数据
	 *
	 * @param {*} value
	 * @memberof PanelFieldController
	 */
	public setData(value: any): void {
		this.data = value;
		if (!this.parentName) {
			return;
		} else {
			let parentItem =
				this.panel.store.layoutModelDetails[`${this.parentName}_${this.index}`];
			// 尝试找
			if (!parentItem) {
				parentItem = this.panel.store.layoutModelDetails[this.parentName];
			}
			if (parentItem) {
				const key = this.dataItemName ? this.dataItemName : this.name;
				parentItem.setData({ name: key, value: value, index: this.index });
			}
		}
	}

	/**
	 * 重置当前项数据
	 *
	 * @memberof PanelFieldController
	 */
	public reSetData() {
		this.data = null;
	}

	/**
	 * 获取元素样式（直接内容元素，包含内容盒子 大小/边距/内容 的样式）
	 *
	 * @return {*}
	 * @memberof PanelFieldController
	 */
	public getElementStyle() {
		const elementStyle = {};
		Object.assign(elementStyle, this.getBoxSizeStyle());
		Object.assign(elementStyle, this.getBoxSpacingStyle());
		Object.assign(elementStyle, this.getBoxContentStyle());
		Object.assign(elementStyle, this.getBoxSelfAlignStyle());
		return elementStyle;
	}

	/**
	 * 获取文本内容布局样式
	 *
	 * @protected
	 * @memberof PanelFieldController
	 */
	protected getBoxContentStyle() {
		const contentStyle = {};
		// 文本换行模式
		if (this.wrapMode) {
			switch (this.wrapMode) {
				case 'NOWRAP':
					Object.assign(contentStyle, {
						'white-space': 'nowrap',
						'text-overflow': 'ellipsis',
						overflow: 'hidden',
					});
					break;
				default:
					break;
			}
		}
		// 文本水平对齐
		if (this.hAlign) {
			switch (this.hAlign) {
				case 'LEFT':
					Object.assign(contentStyle, { 'text-align': 'left' });
					break;
				case 'CENTER':
					Object.assign(contentStyle, { 'text-align': 'center' });
					break;
				case 'RIGHT':
					Object.assign(contentStyle, { 'text-align': 'right' });
					break;
				case 'JUSTIFY':
					Object.assign(contentStyle, { 'text-align': 'justify' });
					break;
				default:
					break;
			}
		}
		// 文本垂直对齐
		if (this.vAlign) {
			switch (this.vAlign) {
				case 'TOP':
					Object.assign(contentStyle, { 'vertical-align': 'top' });
					break;
				case 'MIDDLE':
					Object.assign(contentStyle, { 'vertical-align': 'middle' });
					break;
				case 'BOTTOM':
					Object.assign(contentStyle, { 'vertical-align': 'bottom' });
					break;
				default:
					break;
			}
		}
		return contentStyle;
	}
}
